﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

namespace Skupine_Web_Part.SkupineWP
{
    class SkupineEditor : EditorPart
    {
        TextBox ldap;
        RadioButtonList opcije;
        TextBox ou;
        CheckBox executionTime;
        CheckBox numberOfObjects;
        CheckBox errors;


        protected override void CreateChildControls()
        {
            opcije = new RadioButtonList() { ID = "opcijeId" };

            ListItem item = new ListItem("Display site users");
            opcije.Items.Add(item);
            item = new ListItem("Display users and groups from specific OU");
            opcije.Items.Add(item);
            opcije.AutoPostBack = true;
            opcije.SelectedIndexChanged += opcije_SelectedIndexChanged;

            ldap = new TextBox() { ID = "ldapID" };
            ou = new TextBox() { ID = "ouId" };

            executionTime = new CheckBox() { ID = "executionTimeId" };
            executionTime.Checked = true;

            numberOfObjects = new CheckBox() { ID = "numberOfObjectsId" };
            numberOfObjects.Checked = true;

            errors = new CheckBox() { ID = "errorsId" };

           this.Controls.Add(new Label()
                {
                    Text = "<h3 style='text-align:center'><img src='/_layouts/images/Skupine web part/XnetLogo32.png' />Xnet Display AD Group Members</h3><hr/>"
                });

            this.Controls.Add(new Label()
            {
                Text = "<br/>Enter FQDN of your domain<br/>",
                AssociatedControlID = ldap.ID
            });

            this.Controls.Add(ldap);

            this.Title = "Properties";

            this.Controls.Add(new Label()
                {
                    Text = "<br/>Choose an option for displaying users and groups<br/>",
                    AssociatedControlID = opcije.ID
                });

            this.Controls.Add(opcije);

            this.Controls.Add(new Label()
                {
                    Text = "<br/>Enter OU where you want to display users and groups from<br/><span style='font-style:Italic'>If you select this option, site users and groups will not be displayed</span><br/>",
                    AssociatedControlID = ou.ID
                });
            this.Controls.Add(ou);

            this.Controls.Add(new Label()
                {
                    Text = "<br/><span style='font-style:Italic'>Enter OU filter in following format: <strong>OU=Employees,DC=xnetdev,dc=si</strong></span><br/>If you leave OU filter blank, you will query whole domain<br/><hr/>"
                });
            this.Controls.Add(new Label()
                {
                    Text = "<br/>Display execution time in web part<br/>",
                    AssociatedControlID = executionTime.ID
                });
            this.Controls.Add(executionTime);
            this.Controls.Add(new Label()
            {
                Text = "<br/>Display number of objects<br/>",
                AssociatedControlID = numberOfObjects.ID
            });
            this.Controls.Add(numberOfObjects);

            this.Controls.Add(new Label()
                {
                    Text = "<br/>Display errors (debug mode)<br/>",
                    AssociatedControlID = errors.ID
                });
            this.Controls.Add(errors);

            base.CreateChildControls();
            this.ChildControlsCreated = true;
        }

        void opcije_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (opcije.SelectedValue == "Display users and groups from specific OU")
            {
                ou.Enabled = true;

            }
            else
            {
                ou.Enabled = false;
                ou.Text = "";
            }

        }
        public override void SyncChanges()
        {
            EnsureChildControls();
            SkupineWP webPart = this.WebPartToEdit as SkupineWP;
            if (webPart != null)
            {
                ldap.Text = webPart.ldapQuery;
                opcije.SelectedValue = webPart.opcije;
                ou.Text = webPart.ou;
                numberOfObjects.Checked = webPart.NumberOfObjects;
                executionTime.Checked = webPart.ExecutionTime;
                errors.Checked = webPart.ShowErrors;


            }
        }
        public override bool ApplyChanges()
        {
            EnsureChildControls();
            SkupineWP webPart = this.WebPartToEdit as SkupineWP;
            if (webPart != null)
            {
                //webPart.username = username.Text;
                //webPart.password = password.Text;
                //webPart.DCName = dc.Text;
                webPart.ldapQuery = ldap.Text;
                webPart.opcije = opcije.SelectedValue;
                webPart.ou = ou.Text;
                webPart.ExecutionTime = executionTime.Checked;
                webPart.NumberOfObjects = numberOfObjects.Checked;
                webPart.ShowErrors = errors.Checked;
            }
            return true;
        }
    }
}
